﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLDBHYT.DAO;
using QLDBHYT.VO;
using QLDBHYT.BUS;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;


namespace QLDBHYT
{
    public partial class KCBNgoaiTru : Form
    {
        private QLDBHYT.VO.P1hVO p1hVO;

       
        private P1hBUS p1hBUS;
        SttDAO sttDAO;
        SttVO sttVO;
        PhieunhapBUS phieunhapBUS;
        private List<DoituongVO> lstDoituong;
        private List<P1duVO> lstP1duVO;
        private List<P1dVO> lstP1dVO;
        private string vanchuyen;
        public KCBNgoaiTru()
        {
            InitializeComponent();
        }
        internal QLDBHYT.VO.P1hVO P1hVO
        {
            get { return p1hVO; }
            set { p1hVO = value; }
        }
        private void KCBNgoaiTru_Load(object sender, EventArgs e)
        {
            p1hBUS = new P1hBUS();
            phieunhapBUS = new PhieunhapBUS();
            sttDAO = new SttDAO();
            GetKhoakham();
            GetThuoc();
            FormatFa();
            lstDoituong = p1hBUS.GetDoiTuong();
            ClearControl();
           
        }
        private bool CheckDoiTuong(string madt,string loaitt)
        {
            var items = from item in lstDoituong
                        where item.Loaitt.Contains(loaitt) && item.Madt == madt
                        select item;

            List<DoituongVO> lstDoituong1 = items.ToList<DoituongVO>() ;
            if (lstDoituong1.Count >= 1)
            {
                vanchuyen = lstDoituong1[0].Vanchuyen;
                return true;
            }
            else
            {
                vanchuyen = "";
                return false;
            }

        }
        private void FormatFa()
        {
            fa.Cols[2].Editor = cboThuoc;
            fa.Cols[2].Editor.Width = 800;
        }
        private void txtMathe_Leave(object sender, EventArgs e)
        {
            P1hVO p1h;
            if (cboDT.SelectedIndex != 0) { return; }
            if (txtMathe.Text.Length  < 15) { 
                MessageBox.Show("Nhập lại mã KCB");
                return; 
            }
            if (!CheckDoiTuong(txtMathe.Text.Substring(0,2),txtMathe.Text.Substring(2,1)))
            {
                MessageBox.Show("Mã đối tượng không hợp lệ");
                return; 
            }
            p1h = p1hBUS.GetMathe(txtMathe.Text);
            txtHoten.Text = p1h.Hoten;
            txtGioitinh.Text = p1h.Gioitinh.ToString();
            txtNamsinh.Text = p1h.Namsinh.ToString();
            txtDiachi.Text = p1h.Diachi;
            txtGTTU.Value = p1h.Gttu;
            txtGTDen.Value = p1h.Gtden;
            txtMatinh.Text = p1h.Tinhkcb;
            txtMaBV.Text = p1h.Makcb;
            txtTenBV.Text = p1h.Benhvien;
           
        }

        private void KCBNgoaiTru_KeyDown(object sender, KeyEventArgs e)
        {
            Modules.SendKey(this, e);
            if (e.KeyCode == Keys.F11)
            {
                Cursor.Current = Cursors.WaitCursor;
                frmThemChiPhi childForm = new frmThemChiPhi();
                childForm.ShowDialog();
                Cursor.Current = Cursors.Default;
                
            }
        }

        private void btnMoi_Click(object sender, EventArgs e)
        {
            ClearControl();
        }
        private void ClearControl()
        {
            lstP1duVO = new List<P1duVO>(12);
            lstP1dVO = new List<P1dVO>();
            txtMathe.Text = "";
            txtMathe.Focus();
            cboDT.SelectedIndex = 0;
            cboDieutri.SelectedIndex = 0;
            cboMakhoa.SelectedIndex = 0;
            txtCongkham.Value = 7000;
            txtNgaykham.Value = DateTime.Now;
            txtNgayTT.Value = DateTime.Now;
            txtGTTU.Value = DateTime.Now;
            txtGTDen.Value = DateTime.Now;
            txtHoten.Text = "";
            txtNamsinh.Text = "";
            txtGioitinh.Text = "";
            txtDiachi.Text = "";
            txtMatinh.Text = "56";
            txtMaBV.Text = "161";
            txtTenBV.Text = "Phßng kh¸m ®a khoa khu vùc 2";
            txtTenbenh.Text = "";
            txtDM.Value = 0;
            txtMau.Value = 0;
            txtDT.Value = 0;
            txtXN.Value = 0;
            txtCDHA.Value = 0;
            txtVanchuyen.Value = 0;
            txtTT.Value = 0;
            txtDVKT.Value = 0;
            txtTieuHao.Value = 0;
            txtThaythe.Value = 0;
            txtThuocK.Value = 0;
            txtTongtien.Value = 0;
            txtTienBHXH.Value = 0;
            txtTienBN.Value = 0;
            txtSophieu.Text = "";
            txtMathe.ReadOnly = false;
            fa.Rows.Count = 1;

        }
        private void GetKhoakham()
        {

            cboMakhoa.ClearItems();
            cboMakhoa.AllowSort = true;
            cboMakhoa.DataSource = p1hBUS.GetKhoaKham();
            cboMakhoa.Splits[0].DisplayColumns[0].Width = 50;
            cboMakhoa.Splits[0].DisplayColumns[1].Width = 400;
           
        }
        private void GetThuoc()
        {
            cboThuoc.ClearItems();
            cboThuoc.AllowSort = true;
            cboThuoc.DataSource = p1hBUS.GetTenHieu(Convert.ToInt32 (Modules.GetKy().Rows[0]["thang"]), 
               Convert.ToInt32(Modules.GetKy().Rows[0]["nam"]));
            cboThuoc.Splits[0].DisplayColumns[0].Width = 400;
            cboThuoc.Splits[0].DisplayColumns[1].Width = 50;
            cboThuoc.Splits[0].DisplayColumns[2].Width = 50;
            
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void SumNhomCP()
        {
            decimal decDM, decMau, decDC, decXN,
                decTNCN, decTT, decDVKTC, decVTYT,
                decTH, decNG, decVC, decVTYTTT, decThuocK; 
                decDM= decMau= decDC= decXN=
                decTNCN= decTT= decDVKTC= decVTYT=
                decTH = decNG = decVC = decVTYTTT = decThuocK=0 ;

            for (int i = 1; i <= fa.Rows.Count - 1; i++)
            {
                if (fa.Rows[i]["mahieu"] != null)
                {
                   
                    switch (fa.Rows[i]["MaNhomCP"].ToString())
                    {
                        case "01":
                            decDM = decDM + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "02":
                            decMau = decMau + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "03":
                            decDC = decDC + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "04":
                            decXN = decXN + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "05":
                            decTNCN = decTNCN + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "06":
                        case "08":
                            decTT = decTT + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "09":
                            decDVKTC = decDVKTC + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "10":
                            decVTYT = decVTYT + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "11":
                            decNG = decNG + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "12":
                            decVC = decVC + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "13":
                            decVTYTTT = decVTYTTT + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "14":
                            decThuocK = decThuocK + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                    }
                }
            }
            txtDM.Value = decDM;
            txtMau.Value = decMau;
            txtDT.Value = decDC;
            txtXN.Value = decXN;
            txtCDHA.Value = decTNCN;
            txtTT.Value = decTT;
            txtDVKT.Value = decDVKTC;
            txtTieuHao.Value = decVTYT;
            txtThaythe.Value = decVTYTTT;
            txtThuocK.Value = decThuocK;
            txtVanchuyen.Value = decVC;
            txtTongtien.Value = decDM + decMau + decDC + decXN +
                decTNCN + decTT + decDVKTC + decVTYT +
                decTH + decNG + decVC + decVTYTTT + decThuocK + Convert.ToDecimal(txtCongkham.Value);
            
           
        }
        private decimal Tyle()
        {
            if (cboDT.SelectedIndex != 0) { return 0; }
            if (txtMathe.Text.Length < txtMathe.MaxLength)
            {
                MessageBox.Show("Nhập lại số KCB");
                return 0;
            }
            if (cboDieutri.SelectedIndex == 1)
            {
                return Convert.ToDecimal(0.7);    
            }
            if(Convert.ToDecimal(txtTongtien.Value) <= 157500)
            {
                return 1;
            }
            switch (txtMathe.Text.Substring(2, 1).ToString())
            {
                case "1":
                case "2":
                case "3":
                    return 1;
                case "4":
                case "5":
                    return Convert.ToDecimal(0.95);               
                case "6":
                case "7":
                    return Convert.ToDecimal(0.8);
                
            }
            return 1;
        }
        private void GiaBHXH(int e)
        {
            decimal decBHXH=0;
            if (cboDT.SelectedIndex == 0)
            {
                if (txtMathe.Text.Length < txtMathe.MaxLength)
                {
                    MessageBox.Show("Nhập lại số KCB");
                    return;
                }
                for (int i = 1; i < fa.Rows.Count; i++)
                {
                    fa.Rows[i]["tienbhxh"] = Convert.ToInt32(fa.Rows[i]["SL"]) * Convert.ToDecimal(fa.Rows[i]["giabhxh"]) * Tyle();
                    decBHXH = decBHXH + Convert.ToDecimal(fa.Rows[i]["tienbhxh"]);
                    fa.Rows[i]["TienBN"] = Convert.ToDecimal(fa.Rows[i]["tongtien"]) - Convert.ToDecimal(fa.Rows[i]["tienbhxh"]);
                }

                txtTienBHXH.Value = decBHXH + Convert.ToDecimal(txtCongkham.Value) * Tyle();
                txtTienBN.Value = Convert.ToDecimal(txtTongtien.Value) - Convert.ToDecimal(txtTienBHXH.Value);
            }
            else //khong kham bang the bhyt
            {
                for (int i = 1; i < fa.Rows.Count; i++)
                {                    
                    fa.Rows[i]["TienBN"] = Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                }
                txtTienBN.Value = Convert.ToDecimal(txtTongtien.Value);
            }

        }
        private void fa_AfterEdit(object sender, C1.Win.C1FlexGrid.RowColEventArgs e)
        {
            if(fa.Cols[e.Col].Name == "SL")
            {
                fa.Rows[e.Row]["tongtien"] = Convert.ToInt32(fa.Rows[e.Row]["SL"]) * Convert.ToDecimal(fa.Rows[e.Row]["gia"]) ;
                SumNhomCP();
                GiaBHXH(e.Row);
                
                
            }
            if (fa.Cols[e.Col].Name == "TenCP")
            {
                for (int i = 1; i < fa.Rows.Count; i++)
                {
                    if (e.Row != i && fa.Rows[i]["mahieu"] != null && fa.Rows[e.Row]["TenCP"].ToString().Trim() == fa.Rows[i]["TenCP"].ToString().Trim())
                    {
                        MessageBox.Show("Mã hiệu bị trùng vui lòng nhập lại");
                        fa.Rows[e.Row]["TenCP"] = "";
                        return;
                    }

                }
                fa.Rows[e.Row]["gia"] = cboThuoc.Columns["gia"].Value;
                fa.Rows[e.Row]["giabhxh"] = cboThuoc.Columns["giabhxh"].Value;
                fa.Rows[e.Row]["MaNhomCP"] = cboThuoc.Columns["sttnh"].Value;
                fa.Rows[e.Row]["mahieu"] = cboThuoc.Columns["mahieu"].Value;
                fa.Rows[e.Row]["ktc"] = cboThuoc.Columns["ktc"].Value;
                fa.Rows[e.Row]["dvt"] = cboThuoc.Columns["dvt"].Value;
                switch (fa.Rows[e.Row]["MaNhomCP"].ToString())
                {
                    case "01":
                        fa.Rows[e.Row]["NhomCP"] = "Trong danh mục";
                        break;
                    case "02":
                        fa.Rows[e.Row]["NhomCP"] = "Máu, Đạm";
                        break;
                    case "03":
                        fa.Rows[e.Row]["NhomCP"] = "Dịch truyền";
                        break;
                    case "04":
                        fa.Rows[e.Row]["NhomCP"] = "Xét nghiệm";
                        break;
                    case "05":
                        fa.Rows[e.Row]["NhomCP"] = "CĐHA, TDCN";
                        break;
                    case "06":
                    case "08":
                        fa.Rows[e.Row]["NhomCP"] = "Thủ thuật";
                        break;
                    case "09":
                        fa.Rows[e.Row]["NhomCP"] = "DVKT cao";
                        break;
                    case "10":
                        fa.Rows[e.Row]["NhomCP"] = "VTYT tiêu hao";
                        break;
                    case "11":
                        fa.Rows[e.Row]["NhomCP"] = "Ngày giường";
                        break;
                    case "12":
                        fa.Rows[e.Row]["NhomCP"] = "Vận chuyển";
                        break;
                    case "13":
                        fa.Rows[e.Row]["NhomCP"] = "VTYT thay thế";
                        break;
                    case "14":
                        fa.Rows[e.Row]["NhomCP"] = "Thuốc K";
                        break;
                }
            }
        }
        private void btnGhi_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (txtMathe.ReadOnly == false)
            {
                InsertKCB();
            }
            else
            {
                UpdateKCB();
            }
            UpdateFA();
            this.Cursor = Cursors.Default;

        }   
        private void UpdateKCB()
        {
            if (CheckSoLuongTon() == false) { return; }
            SetValueToP1H(sttVO);
            SetValueToP1DU(sttVO);
            CheckUpdateP1D();
            p1hBUS.UpdateRecord(p1hVO, lstP1duVO, lstP1dVO);
            UpdateListP1DVO();
            GetThuoc();
        }
        private void UpdateListP1DVO()
        {
            int count = lstP1dVO.Count;
            int i = 0;
            while (i < count)
            {
                if (lstP1dVO[i].Flag == 0)
                {
                    lstP1dVO.RemoveAt(i);
                    count--;
                }
                else
                {
                    lstP1dVO[i].Flag = 0;
                    i++;
                }
                
            }
            
        }
        private void InsertKCB()
        {
            p1hVO = new P1hVO();
            if (CheckSoLuongTon() == false) { return; }
            sttVO = sttDAO.GetStt(Modules.nam);
            SetValueToP1H(sttVO);
            SetValueToP1DU(sttVO);
            SetValueToP1D(sttVO);
            p1hBUS.InsertRecord(p1hVO, lstP1duVO, lstP1dVO);
            sttDAO.UpdateStt(Modules.nam, sttVO);
            txtSophieu.Text = string.Format("{0:000000}", sttVO.Phieung);
            GetThuoc();
            txtMathe.ReadOnly = true;
           
        }
        private void UpdateFA()
        {
            for (int i = 1; i < fa.Rows.Count; i++)
            {
                if (fa.Rows[i]["mahieu"] != null)
                {
                    fa.Rows[i]["slcu"] = fa.Rows[i]["sl"];
                }
            }
        }
        private bool CheckSoLuongTon()
        {
            List<PhieunhapVO> lstPhieuNhap;
            string condition;
            for (int i = 1; i < fa.Rows.Count; i++)
            {
                if (fa.Rows[i]["mahieu"] != null && fa.Rows[i]["manhomcp"].ToString() == "01")
                {
                    condition = " and a.ma_hieu = '" + fa.Rows[i]["mahieu"].ToString() + "'";
                    lstPhieuNhap = phieunhapBUS.GetListPhieuNhap(Convert.ToInt32(Modules.thang),
                    Modules.nam, fa.Rows[i]["manhomcp"].ToString(),
                    "", "", condition);

                    if (lstPhieuNhap[0].Slcuoiky + Convert.ToInt16(fa.Rows[i]["slcu"]) < Convert.ToInt16(fa.Rows[i]["sl"]))
                    {
                        MessageBox.Show("Số lượng thuốc " + fa.Rows[i]["TenCP"].ToString() + " còn tồn " + lstPhieuNhap[0].Slcuoiky);
                        return false;
                    }

                   
                }
            }
            return true;

        }
        private void SetValueToP1D(SttVO sttVO)
        {
            P1dVO p1dVO;

            for (int i = 1; i < fa.Rows.Count; i++)
            {
                if (fa.Rows[i]["mahieu"] != null)
                {
                    
                    p1dVO = new P1dVO();
                    p1dVO.Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
                    p1dVO.Ctid0 = string.Format("{0:000000000000000}", sttVO.Ctid0);
                    p1dVO.Sttnh = fa.Rows[i]["manhomcp"].ToString();
                    p1dVO.Mahieu = fa.Rows[i]["mahieu"].ToString();
                    p1dVO.Tyle = 100;
                    p1dVO.Ktc = fa.Rows[i]["ktc"].ToString();
                    p1dVO.Sl = Convert.ToInt16(fa.Rows[i]["sl"]);
                    p1dVO.Gia = Convert.ToInt32(fa.Rows[i]["gia"]);
                    p1dVO.Giabhxh = Convert.ToInt32(fa.Rows[i]["giabhxh"]);
                    p1dVO.Tongtien = Convert.ToInt32(fa.Rows[i]["tongtien"]);
                    p1dVO.Tienbhxh = Convert.ToInt32(fa.Rows[i]["Tienbhxh"]);
                    p1dVO.Tienchenhlech = 0;//Convert.ToInt16(fa.Rows[i]["Tienchenhlech"]);
                    p1dVO.Tien_bn = Convert.ToInt32(fa.Rows[i]["tienbn"]);
                    p1dVO.Dvt = fa.Rows[i]["dvt"].ToString();
                    p1dVO.Loai = 1;
                    p1dVO.Bacsy = "";
                    p1dVO.Khoa = "";
                    p1dVO.Chon = 0;
                    p1dVO.Flag = 0;
                    lstP1dVO.Add(p1dVO);
                    sttVO.Ctid0++;
                }
            }

        }
        private void CheckUpdateP1D()
        {
            P1dVO p1dVO;
            bool flag = false;
            for (int i = 1; i < fa.Rows.Count; i++)
            {
                if (fa.Rows[i]["mahieu"] != null && fa.Rows[i]["mahieu"] != "")
                {
                    flag = false;
                    foreach (P1dVO item in lstP1dVO)
                    {
                        //so khong thay doi
                        if (item.Mahieu == fa.Rows[i]["mahieu"].ToString())
                        {
                            if (item.Sl == Convert.ToInt16(fa.Rows[i]["sl"]))
                            {
                                item.Flag = 2;
                                item.Tienbhxh = Convert.ToInt32(fa.Rows[i]["Tienbhxh"]);
                                item.Tien_bn = Convert.ToInt32(fa.Rows[i]["tienbn"]);
                                flag = true;
                                break;
                            }
                            else //so luong thay doi
                            {
                                
                                item.Sl = Convert.ToInt16(fa.Rows[i]["sl"]);
                                item.Tongtien = Convert.ToInt32(fa.Rows[i]["tongtien"]);
                                item.Tienbhxh = Convert.ToInt32(fa.Rows[i]["Tienbhxh"]);
                                item.Tienchenhlech = 0;//Convert.ToInt16(fa.Rows[i]["Tienchenhlech"]);
                                item.Tien_bn = Convert.ToInt32(fa.Rows[i]["tienbn"]);
                                item.Flag = 2;
                                flag = true;    
                                break;
                            }
                        }
                    }
                    // them moi du lieu
                    if ( flag == false)
                    {
                        p1dVO = new P1dVO();
                        p1dVO.Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
                        p1dVO.Ctid0 = string.Format("{0:000000000000000}", sttDAO.GetStt_Ct_id2(Modules.nam));
                        p1dVO.Sttnh = fa.Rows[i]["manhomcp"].ToString();
                        p1dVO.Mahieu = fa.Rows[i]["mahieu"].ToString();
                        p1dVO.Tenhieu = fa.Rows[i]["tencp"].ToString();
                        p1dVO.Tyle = 100;
                        p1dVO.Ktc = fa.Rows[i]["ktc"].ToString();
                        p1dVO.Sl = Convert.ToInt16(fa.Rows[i]["sl"]);
                        p1dVO.Gia = Convert.ToInt32(fa.Rows[i]["gia"]);
                        p1dVO.Giabhxh = Convert.ToInt32(fa.Rows[i]["giabhxh"]);
                        p1dVO.Tongtien = Convert.ToInt32(fa.Rows[i]["tongtien"]);
                        p1dVO.Tienbhxh = Convert.ToInt32(fa.Rows[i]["Tienbhxh"]);
                        p1dVO.Tienchenhlech = 0;//Convert.ToInt16(fa.Rows[i]["Tienchenhlech"]);
                        p1dVO.Tien_bn = Convert.ToInt32(fa.Rows[i]["tienbn"]);
                        p1dVO.Loai = 1;
                        p1dVO.Bacsy = "";
                        p1dVO.Khoa = "";
                        p1dVO.Chon = 0;         
                        p1dVO.Flag = 3;
                        lstP1dVO.Add(p1dVO);
                        sttVO.Ctid0++;
                    }

                        
                }
                    
            }
        }
        private void SetValueToP1DU(SttVO sttVO)
        {
            lstP1duVO = new List<P1duVO>(11);
            lstP1duVO.Clear();
            P1duVO p1hVOTam = new P1duVO();
            lstP1duVO.Add(p1hVOTam);
            lstP1duVO[0].Sttnh = "01";

            lstP1duVO[0].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP1duVO[0].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2);
            lstP1duVO[0].Tongtien = Convert.ToDecimal(txtDM.Value);
            lstP1duVO[0].Tienbn = Convert.ToDecimal(txtDM.Value)*(1- Tyle());

            p1hVOTam = new P1duVO();
            lstP1duVO.Add(p1hVOTam);
            lstP1duVO[1].Sttnh = "02";
            lstP1duVO[1].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP1duVO[1].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2+1);
            lstP1duVO[1].Tongtien = Convert.ToDecimal(txtMau.Value);
            lstP1duVO[1].Tienbn = Convert.ToDecimal(txtMau.Value)*(1- Tyle());

            p1hVOTam = new P1duVO();
            lstP1duVO.Add(p1hVOTam);
            lstP1duVO[2].Sttnh = "03";
            lstP1duVO[2].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP1duVO[2].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2+2);
            lstP1duVO[2].Tongtien = Convert.ToDecimal(txtDT.Value);
            lstP1duVO[2].Tienbn = Convert.ToDecimal(txtDT.Value)*(1- Tyle());

            p1hVOTam = new P1duVO();
            lstP1duVO.Add(p1hVOTam);
            lstP1duVO[3].Sttnh = "04";
            lstP1duVO[3].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP1duVO[3].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2+3);
            lstP1duVO[3].Tongtien = Convert.ToDecimal(txtXN.Value);
            lstP1duVO[3].Tienbn = Convert.ToDecimal(txtXN.Value)*(1- Tyle());

            p1hVOTam = new P1duVO();
            lstP1duVO.Add(p1hVOTam);
            lstP1duVO[4].Sttnh = "05";
            lstP1duVO[4].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP1duVO[4].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2+4);
            lstP1duVO[4].Tongtien = Convert.ToDecimal(txtCDHA.Value);
            lstP1duVO[4].Tienbn = Convert.ToDecimal(txtCDHA.Value)*(1- Tyle());

            p1hVOTam = new P1duVO();
            lstP1duVO.Add(p1hVOTam);
            lstP1duVO[5].Sttnh = "07";
            lstP1duVO[5].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP1duVO[5].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2+5);
            lstP1duVO[5].Tongtien = Convert.ToDecimal(txtCongkham.Value);
            lstP1duVO[5].Tienbn = Convert.ToDecimal(txtCongkham.Value)*(1- Tyle());

            p1hVOTam = new P1duVO();
            lstP1duVO.Add(p1hVOTam);
            lstP1duVO[6].Sttnh = "08";
            lstP1duVO[6].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP1duVO[6].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2+6);
            lstP1duVO[6].Tongtien = Convert.ToDecimal(txtTT.Value);
            lstP1duVO[6].Tienbn = Convert.ToDecimal(txtTT.Value)*(1- Tyle());

            p1hVOTam = new P1duVO();
            lstP1duVO.Add(p1hVOTam);
            lstP1duVO[7].Sttnh = "09";
            lstP1duVO[7].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP1duVO[7].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2+7);
            lstP1duVO[7].Tongtien = Convert.ToDecimal(txtDVKT.Value);
            lstP1duVO[7].Tienbn = Convert.ToDecimal(txtDVKT.Value)*(1- Tyle());

            p1hVOTam = new P1duVO();
            lstP1duVO.Add(p1hVOTam);
            lstP1duVO[8].Sttnh = "10";
            lstP1duVO[8].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP1duVO[8].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2+8);
            lstP1duVO[8].Tongtien = Convert.ToDecimal(txtTieuHao.Value);
            lstP1duVO[8].Tienbn = Convert.ToDecimal(txtTieuHao.Value)*(1- Tyle());

            p1hVOTam = new P1duVO();
            lstP1duVO.Add(p1hVOTam);
            lstP1duVO[9].Sttnh = "12";
            lstP1duVO[9].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP1duVO[9].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2+9);
            lstP1duVO[9].Tongtien = Convert.ToDecimal(txtVanchuyen.Value);
            lstP1duVO[9].Tienbn = Convert.ToDecimal(txtVanchuyen.Value)*(1- Tyle());

            p1hVOTam = new P1duVO();
            lstP1duVO.Add(p1hVOTam);
            lstP1duVO[10].Sttnh = "13";
            lstP1duVO[10].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP1duVO[10].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2+10);
            lstP1duVO[10].Tongtien = Convert.ToDecimal(txtThaythe.Value );
            lstP1duVO[10].Tienbn = Convert.ToDecimal(txtThaythe.Value )*(1- Tyle());

            p1hVOTam = new P1duVO();
            lstP1duVO.Add(p1hVOTam);
            lstP1duVO[11].Sttnh = "14";
            lstP1duVO[11].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP1duVO[11].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2+11);
            lstP1duVO[11].Tongtien = Convert.ToDecimal(txtThuocK.Value);
            lstP1duVO[11].Tienbn = Convert.ToDecimal(txtThuocK.Value)*(1- Tyle());
             
        }
        private void SetValueToP1H(SttVO sttVO)
        {            
            p1hVO.Ctid = string.Format("{0:000000000000000}",sttVO.Ctid);
            p1hVO.Loai = "01";
            p1hVO.Matinh = "56";
            p1hVO.Sophieu = string.Format("{0:000000}", sttVO.Phieung);
            p1hVO.Benhvien = "161";
            p1hVO.Makhoa = cboMakhoa.Columns[0].Value.ToString();
            p1hVO.Tenkhoa = cboMakhoa.Columns[1].Value.ToString();
            p1hVO.Mathe = txtMathe.Text;
            p1hVO.Namphah = "";
            if (txtMathe.Text != "")
            {
                p1hVO.Madt = txtMathe.Text.ToString().Substring(0, 2);
                p1hVO.Loaitt = txtMathe.Text.ToString().Substring(2, 1);
                p1hVO.Mabhxh = txtMathe.Text.Substring(3, 2);
                p1hVO.Vanchuyen = vanchuyen;
            }
            else
            {
                p1hVO.Madt = "";
                p1hVO.Loaitt = "";
                p1hVO.Mabhxh = "";
                p1hVO.Vanchuyen = "";
            }

            p1hVO.Doituong = Convert.ToInt16(cboDT.Text.ToString().Substring(0, 1));
            p1hVO.Loaitt1 = 1;
            p1hVO.Hoten = txtHoten.Text;
            p1hVO.Gioitinh = Convert.ToInt16(txtGioitinh.Text);
            p1hVO.Namsinh = Convert.ToInt32(txtNamsinh.Text);
            p1hVO.Diachi = txtDiachi.Text;
            p1hVO.Tinhkcb = txtMatinh.Text;
            p1hVO.Makcb = txtMaBV.Text;
            p1hVO.Noikham = txtTenBV.Text;
            p1hVO.Ngaykham = txtNgaykham.Text.ToString().Substring(6, 4) + "/" + txtNgaykham.Text.ToString().Substring(3, 2) + "/" + txtNgaykham.Text.ToString().Substring(0, 2);
            p1hVO.Gttu = txtGTTU.Text.ToString().Substring(6, 4) + "/" + txtGTTU.Text.ToString().Substring(3, 2) + "/" + txtGTTU.Text.ToString().Substring(0, 2);
            p1hVO.Gtden = txtGTDen.Text.ToString().Substring(6, 4) + "/" + txtGTDen.Text.ToString().Substring(3, 2) + "/" + txtGTDen.Text.ToString().Substring(0, 2);
            p1hVO.Ngaytt = txtNgayTT.Text.ToString().Substring(6, 4) + "/" + txtNgayTT.Text.ToString().Substring(3, 2) + "/" + txtNgayTT.Text.ToString().Substring(0, 2);
            p1hVO.Tenbenh = txtTenbenh.Text;
          
            p1hVO.Noiden = txtNcd.Text;
            p1hVO.Ngay1 = txtNgayTT.Text.ToString().Substring(6, 4) + "/" + txtNgayTT.Text.ToString().Substring(3, 2) + "/" + txtNgayTT.Text.ToString().Substring(0, 2);
            p1hVO.Ngay2 = txtNgayTT.Text.ToString().Substring(6, 4) + "/" + txtNgayTT.Text.ToString().Substring(3, 2) + "/" + txtNgayTT.Text.ToString().Substring(0, 2);
            p1hVO.Ngay3 = txtNgayTT.Text.ToString().Substring(6, 4) + "/" + txtNgayTT.Text.ToString().Substring(3, 2) + "/" + txtNgayTT.Text.ToString().Substring(0, 2);
            p1hVO.Thaisan = 0;
            p1hVO.Ngaylv = txtNgaykham.Text.ToString().Substring(6, 4) + "/" + txtNgaykham.Text.ToString().Substring(3, 2) + "/" + txtNgaykham.Text.ToString().Substring(0, 2);
            p1hVO.Usernam = Properties.Settings.Default.UserName;
            p1hVO.Excel = 0;
            p1hVO.Dieutri = cboDieutri.Text.Substring(0, 1);
            p1hVO.Ketqua = 0;
            p1hVO.Benhphu = "";
            p1hVO.Dain = "";
            p1hVO.Ngaysinh = 0;
            p1hVO.Thangsinh = 0;
            p1hVO.Maicd = "";
            p1hVO.Benhan = "";
            p1hVO.Lenhdx = "";
            p1hVO.Madv = "";
            p1hVO.Macb = "";
          
        }
        public void SetValueToControl()
        {
            sttVO = new SttVO();
            sttVO.Ctid = Convert.ToDecimal(p1hVO.Ctid);
            sttVO.Phieung = Convert.ToDecimal(p1hVO.Sophieu);
            txtSophieu.Text = p1hVO.Sophieu;
            cboMakhoa.SelectedValue = p1hVO.Makhoa;
            txtMathe.Text = p1hVO.Mathe;
            txtMathe.ReadOnly = true;
            vanchuyen = p1hVO.Vanchuyen;
            txtHoten.Text = p1hVO.Hoten;
            txtGioitinh.Text = p1hVO.Gioitinh.ToString();
            txtNamsinh.Text = p1hVO.Namsinh.ToString();
            txtDiachi.Text = p1hVO.Diachi;
            txtMatinh.Text = p1hVO.Tinhkcb;
            txtMaBV.Text = p1hVO.Makcb;
            txtTenBV.Text = p1hVO.Noikham;
            txtNgaykham.Value = p1hVO.Ngaykham;
            txtGTTU.Value = p1hVO.Gttu;
            txtGTDen.Value = p1hVO.Gtden;
            txtNgayTT.Value = p1hVO.Ngaytt;
            txtTenbenh.Text = p1hVO.Tenbenh;
            txtNcd.Text = p1hVO.Noiden;
            if (p1hVO.Doituong >= 3)
            {
                cboDT.SelectedIndex = Convert.ToInt16(p1hVO.Doituong) - 2;
            }
            else
            {
                cboDT.SelectedIndex = Convert.ToInt16(p1hVO.Doituong) - 1;
            }

           
            if (p1hVO.Dieutri == " ")
            {
                cboDieutri.SelectedIndex = 0;
            }
            else
            {
                cboDieutri.SelectedIndex = Convert.ToInt16(p1hVO.Dieutri) + 1;
            }
            
        }
        public void SetValueToControlP1DU()
        {

            lstP1duVO = p1hBUS.GetListP1DU("p1_du", P1hVO.Ctid);
            sttVO.Ctid2 = Convert.ToDecimal(lstP1duVO[0].Ctid2);
            txtDM.Value = lstP1duVO[0].Tongtien;
            txtMau.Value = lstP1duVO[1].Tongtien;
            txtDT.Value = lstP1duVO[2].Tongtien;
            txtXN.Value = lstP1duVO[3].Tongtien;
            txtCDHA.Value = lstP1duVO[4].Tongtien;
            txtCongkham.Value = lstP1duVO[5].Tongtien;
            txtTT.Value = lstP1duVO[6].Tongtien;
            txtDVKT.Value = lstP1duVO[7].Tongtien;
            txtTieuHao.Value = lstP1duVO[8].Tongtien;
            txtVanchuyen.Value = lstP1duVO[9].Tongtien;
            txtThaythe.Value = lstP1duVO[10].Tongtien;
            txtThuocK.Value = lstP1duVO[11].Tongtien;

           
        }
        public void SetValueToControlP1D()
        {

            lstP1dVO = p1hBUS.GetListP1D("p1_d", P1hVO.Ctid);
            fa.Rows.Count = 1;
            decimal decBHXH = 0;
            int i = 1;
            foreach (P1dVO p1dvo in lstP1dVO)
            {
                fa.Rows.Count = fa.Rows.Count + 1;
                fa.Rows[i]["manhomcp"] = p1dvo.Sttnh;
                fa.Rows[i]["tencp"] = p1dvo.Tenhieu;
                fa.Rows[i]["mahieu"] = p1dvo.Mahieu;
                fa.Rows[i]["ktc"] = p1dvo.Ktc;
                fa.Rows[i]["sl"] = p1dvo.Sl;
                fa.Rows[i]["slcu"] = p1dvo.Sl;
                fa.Rows[i]["gia"] = p1dvo.Gia;
                fa.Rows[i]["giabhxh"] = p1dvo.Giabhxh;
                fa.Rows[i]["tongtien"] = p1dvo.Tongtien;
                fa.Rows[i]["Tienbhxh"] = p1dvo.Tienbhxh;
                fa.Rows[i]["dvt"] = p1dvo.Dvt;
                decBHXH = decBHXH + Convert.ToDecimal(fa.Rows[i]["tienbhxh"]);
                fa.Rows[i]["tienbn"] = p1dvo.Tien_bn;
                switch (fa.Rows[i]["MaNhomCP"].ToString())
                {
                    case "01":
                        fa.Rows[i]["NhomCP"] = "Trong danh mục";
                        break;
                    case "02":
                        fa.Rows[i]["NhomCP"] = "Máu, Đạm";
                        break;
                    case "03":
                        fa.Rows[i]["NhomCP"] = "Dịch truyền";
                        break;
                    case "04":
                        fa.Rows[i]["NhomCP"] = "Xét nghiệm";
                        break;
                    case "05":
                        fa.Rows[i]["NhomCP"] = "CĐHA, TDCN";
                        break;
                    case "06":
                    case "08":
                        fa.Rows[i]["NhomCP"] = "Thủ thuật";
                        break;
                    case "09":
                        fa.Rows[i]["NhomCP"] = "DVKT cao";
                        break;
                    case "10":
                        fa.Rows[i]["NhomCP"] = "VTYT tiêu hao";
                        break;
                    case "11":
                        fa.Rows[i]["NhomCP"] = "Ngày giường";
                        break;
                    case "12":
                        fa.Rows[i]["NhomCP"] = "Vận chuyển";
                        break;
                    case "13":
                        fa.Rows[i]["NhomCP"] = "VTYT thay thế";
                        break;
                    case "14":
                        fa.Rows[i]["NhomCP"] = "Thuốc K";
                        break;
                }
                i++;
                
            }
            txtDM.Value = lstP1duVO[0].Tongtien;
            txtMau.Value = lstP1duVO[1].Tongtien;
            txtDT.Value = lstP1duVO[2].Tongtien;
            txtXN.Value = lstP1duVO[3].Tongtien;
            txtCDHA.Value = lstP1duVO[4].Tongtien;
            txtCongkham.Value = lstP1duVO[5].Tongtien;
            txtTT.Value = lstP1duVO[6].Tongtien;
            txtDVKT.Value = lstP1duVO[7].Tongtien;
            txtTieuHao.Value = lstP1duVO[8].Tongtien;
            txtVanchuyen.Value = lstP1duVO[9].Tongtien;
            txtThaythe.Value = lstP1duVO[10].Tongtien;
            txtThuocK.Value = lstP1duVO[11].Tongtien;

            txtTongtien.Value = Convert.ToDecimal(txtDM.Value) + Convert.ToDecimal(txtMau.Value) 
                + Convert.ToDecimal(txtDT.Value) + Convert.ToDecimal(txtXN.Value) 
                + Convert.ToDecimal(txtCDHA.Value) + Convert.ToDecimal(txtCongkham.Value) + Convert.ToDecimal(txtTT.Value)
                + Convert.ToDecimal(txtDVKT.Value) + Convert.ToDecimal(txtTieuHao.Value) 
                + Convert.ToDecimal(txtVanchuyen.Value) + Convert.ToDecimal(txtThaythe.Value) 
                + Convert.ToDecimal(txtThuocK.Value);
            txtTienBHXH.Value = decBHXH + Convert.ToDecimal(txtCongkham.Value) * Tyle();
            txtTienBN.Value = Convert.ToDecimal(txtTongtien.Value) - Convert.ToDecimal(txtTienBHXH.Value);
           
        }

        private void fa_AfterDeleteRow(object sender, C1.Win.C1FlexGrid.RowColEventArgs e)
        {
            SumNhomCP();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            p1hBUS.DeleteRecord(p1hVO.Ctid);
            GetThuoc();
            ClearControl();
        }
        private string Muchuong(string loaitt)
        {
            if (loaitt == " ")
            {
                return "0%";
            }
            if (Convert.ToDecimal(txtTongtien.Value) <= 157500)
            {
                return "100%";
            }
            switch (txtMathe.Text.Substring(2, 1).ToString())
            {
                case "1":
                case "2":
                case "3":
                    return "100%";
                case "4":
                case "5":
                    return "95%";
                case "6":
                case "7":
                    return "80%";

            }

            return "100%";
        }
        private void btnIn_Click(object sender, EventArgs e)
        {
            int stt = 1;
            string  strTennhom = "cong kham";
            DataRow newRow = null;
            Dataset.dsBCNX dsBCNX = new Dataset.dsBCNX();
            newRow = dsBCNX.Tables["P1H"].NewRow();
            newRow["sophieu"] = P1hVO.Sophieu;
            newRow["muchuong"] = Muchuong(P1hVO.Loaitt);
            newRow["hoten"] = Converter.TCVN3ToUnicode(P1hVO.Hoten);
            newRow["namsinh"] = p1hVO.Namsinh;
            newRow["lenhdx"] = p1hVO.Noiden;
            newRow["gioitinh"] = p1hVO.Gioitinh;
            newRow["diachi"] = Converter.TCVN3ToUnicode(p1hVO.Diachi);
            newRow["mathe"]= p1hVO.Mathe;
            newRow["gttu"] =  Convert.ToDateTime(p1hVO.Gttu).ToString().Substring(0, 10);
            newRow["gtden"] =  Convert.ToDateTime(p1hVO.Gtden).ToString().Substring(0, 10);
            newRow["makcb"] = p1hVO.Makcb;
            newRow["tinhkcb"] = p1hVO.Tinhkcb;
            newRow["noikham"] = p1hVO.Noikham;
            newRow["ngaykham"] =  Convert.ToDateTime(p1hVO.Ngaykham).ToString().Substring(0,10);
            newRow["ngaytt"] =  Convert.ToDateTime(p1hVO.Ngaytt).ToString().Substring(0, 10);
            newRow["dieutri"] = p1hVO.Dieutri;
            newRow["chuandoan"] = p1hVO.Tenbenh;
            newRow["noikham"] = p1hVO.Noikham;
            newRow["ngaykham"] =  Convert.ToDateTime(p1hVO.Ngaykham).ToString().Substring(0, 10);
            newRow["ngaytt"] =  Convert.ToDateTime(p1hVO.Ngaytt).ToString().Substring(0, 10);
            newRow["dieutri"] = p1hVO.Dieutri;
            newRow["chuandoan"] = p1hVO.Tenbenh;
            newRow["doituong"] = p1hVO.Doituong;
            if (p1hVO.Mathe != "")
            {
                newRow["SOKCB11"] = p1hVO.Mathe.Substring(0, 2);
                newRow["SOKCB12"] = p1hVO.Mathe.Substring(2, 1);
                newRow["SOKCB13"] = p1hVO.Mathe.Substring(3, 2);
                newRow["SOKCB14"] = p1hVO.Mathe.Substring(5, 2);
                newRow["SOKCB15"] = p1hVO.Mathe.Substring(7, 3);
                newRow["SOKCB16"] = p1hVO.Mathe.Substring(10);
            }
            newRow["tongtien"] = Modules.ChangeToCamel(Modules.So_chu(Convert.ToDouble(txtTongtien.Value)).Trim());
            newRow["Tienbhxh"] = Modules.ChangeToCamel(Modules.So_chu(Convert.ToDouble(txtTienBHXH.Value)).Trim());
            if (Convert.ToDouble(txtTienBN.Value) != 0)
            {
                newRow["Tienbn"] = Modules.ChangeToCamel(Modules.So_chu(Convert.ToDouble(txtTienBN.Value)).Trim());
            }
            dsBCNX.Tables["P1H"].Rows.Add(newRow);

            newRow = dsBCNX.Tables["P1D"].NewRow();
            newRow["nhomcp"] = stt;
            newRow["tencp"] = "";
            newRow["dvt"] = "";
            newRow["tongtien"] = Convert.ToDecimal(txtCongkham.Value);
            newRow["Tienbhxh"] = Convert.ToDecimal(txtCongkham.Value) * Tyle();
            newRow["tienbn"] = Convert.ToDecimal(newRow["tongtien"]) - Convert.ToDecimal(newRow["Tienbhxh"]);
            newRow["tennhomcp"] = "Khám bệnh";
            dsBCNX.Tables["P1D"].Rows.Add(newRow);
            var q = lstP1dVO.OrderBy(o => o.Sttnh);
            foreach (P1dVO p1dvo in q)
            {
                newRow = dsBCNX.Tables["P1D"].NewRow();
                if (p1dvo.Sttnh != strTennhom)
                {
                    stt++;
                    strTennhom = p1dvo.Sttnh;
                }
                newRow["nhomcp"] = stt;
                newRow["tencp"] = Converter.TCVN3ToUnicode(p1dvo.Tenhieu);
                newRow["dvt"] = p1dvo.Dvt;
                newRow["sl"] = p1dvo.Sl;
                newRow["gia"] = p1dvo.Gia;
                newRow["tongtien"] = p1dvo.Tongtien;
                newRow["Tienbhxh"] = p1dvo.Tienbhxh;
                newRow["tienbn"] = p1dvo.Tien_bn;
                switch (p1dvo.Sttnh.ToString())
                {
                    case "01":
                        newRow["tennhomcp"] = "Trong danh mục";
                        break;
                    case "02":
                        newRow["tennhomcp"] = "Máu, Đạm";
                        break;
                    case "03":
                        newRow["tennhomcp"] = "Dịch truyền";
                        break;
                    case "04":
                        newRow["tennhomcp"] = "Xét nghiệm";
                        break;
                    case "05":
                        newRow["tennhomcp"] = "CĐHA, TDCN";
                        break;
                    case "06":
                    case "08":
                        newRow["tennhomcp"] = "Thủ thuật";
                        break;
                    case "09":
                        newRow["tennhomcp"] = "DVKT cao";
                        break;
                    case "10":
                        newRow["tennhomcp"] = "VTYT tiêu hao";
                        break;
                    case "11":
                        newRow["tennhomcp"] = "Ngày giường";
                        break;
                    case "12":
                        newRow["tennhomcp"] = "Vận chuyển";
                        break;
                    case "13":
                        newRow["tennhomcp"] = "VTYT thay thế";
                        break;
                    case "14":
                        newRow["tennhomcp"] = "Thuốc K";
                        break;
                }

                dsBCNX.Tables["P1D"].Rows.Add(newRow);
            }
            Cursor.Current = Cursors.Default;
            Report f = new Report();
            ReportDocument cr = new ReportDocument();
            cr.Load("rptKCBNgoaitru.rpt");
            cr.SetDataSource(dsBCNX);
            f.crpView.ReportSource = cr;
            f.Show();         
                
        }

    }
}
